<!-- 撤销-付款平账记录 -->
<template>
    <el-dialog
        custom-class="cancel-pay-back-payment"
        v-el-drag-dialog="{ drag: true, full: false, resize: false }"
        :visible="true"
        :close-on-click-modal="false"
        @close="closeMd"
        :title="`撤销-付款平账记录`"
        width="80%">
        <div ref="dgtools" class="filter-container">
            <div class="dgts-row">
                <el-form 
                    :model="dg.ps" 
                    :inline="true" 
                    @keyup.enter.native="getDgList">
                    <el-button 
                        type="primary" 
                        icon="el-icon-search" 
                        @click="getDgList">
                        搜索
                    </el-button>

                    <el-button 
                        type="primary" 
                        icon="el-icon-check" 
                        @click="cancelBackPayment">
                        确认撤销
                    </el-button>
                </el-form>
            </div>
        </div>
        
        <el-table
            :ref="dg.tbName"
            :data="dg.list"
            row-key="id"
            :height="bodyHeight - 60"
            border fit 
            :row-class-name="rowClassName"
            @header-dragend="dragHeader"
            @selection-change="selRowChange">
            <el-table-column 
                fixed="left" 
                type="selection" 
                align="center" 
                :resizable="false" 
                width="40">
            </el-table-column>
            <el-table-column 
                v-for="(col, index) in dg.columns" 
                :key="index"
                :class-name="dg.dgName"
                :prop="col.field"
                :label="col.label"
                :width="col.width">
                <template slot-scope="{row}">
                <template v-if="col.field == 'addTime'">
                    <span>{{row.addTime}}</span>
                </template>

                <span v-else-if="col.field == 'operUser'">
                    {{row.operUser.realName}}
                </span>

                <span v-else-if="col.field == 'reimUser'">
                    {{row.reimUser.realName}}
                </span>

                <span v-else-if="col.field == 'gainTime'">{{row.addTime.substr(0, 16)}}</span>

                <template v-else>{{ getColVal(row, col.field) }}</template>
                </template>
            </el-table-column>
        </el-table>
    </el-dialog>
</template>

<script>
import DataGridBase from '@/views/mixins/DataGridBase';
import DataGridResize from '@/views/mixins/DataGridResize';
import elDragDialog from '@/directive/el-drag-dialog';

import * as orderSer from "@/api/services/order.service";

export default {
    mixins: [ DataGridBase, DataGridResize],
    directives: { elDragDialog },
    props: {myData: Object},
    data() { 
        let cols = [
            { label: "经办人", field: "operUser", width: 80 },
            { label: "往来客户", field: "reimUser", width: 90 },
            { label: "车牌号", field: "plateNum", width: 70 },
            { label: "摘要", field: "remark", width: 'auto' },
            { label: "借方金额", field: "gathMoney", width: 90 },
            { label: "贷方金额", field: "payMoney", width: 180 },
            { label: "记账时间", field: "gainTime", width: 120 },
            { label: "添加时间", field: "addTime", width: 130 },
        ];
        return {
            // 是否撤销过
            isCancel: false,

            dg: {
                dgName: 'dg',
                tbName: 'cancelPayBackPaymentTb',
                total: 0,
                list: [],
                srcCols: cols,
                columns: [],
                selRows: [],
                ps: {
                    page: 1, rows: 100,
                    // 操作编号 eg:CZ1607999606087,CZ1607999606088
                    operMark: '',
                    // 交易类型 eg:收业务款【SERVICEGATH】同行对冲【OFFSET】同行抵收款【DEBTGATH】同行抵账【DEBTPAY】收上网费【NETGATH】
                    reimType: 'DEBTPAY',
                },
            },

        }
    },
    created() {
        const that = this;

        that.initTbSetData(that.dg.dgName);
        that.getDgList();
    },
    methods:{
        closeMd() {
            // 撤销过，则关闭弹框将刷新父组件列表
            if(this.isCancel) this.$emit('reloadDg');

            this.$emit("myClick", {state: false});
        },
        // 获取-表格列表
        getDgList(){
            const that = this, U = that.U, { list } = that.myData, dg = that.dg, ps = U.scopy(dg.ps);
            
            ps.operMark = list.map(it => it.operMark).join(',');
            orderSer.getCollListRecord(ps).then(res => {
                if(res.code == 1){
                    let list = res.data;
                    for(let i in list){ list[i].dgName = dg.dgName; }
                
                    dg.list = list;
                    dg.total = res.count;
                }else{
                    dg.list = [];
                    dg.total = 0;
                }
            });
        },

        // 撤销平账
        cancelBackPayment(){
            const that = this, U = that.U, rows = that.getSeledRows();

            if(rows.length !== 1){
                U.qerror(`${rows.length == 0 ? '请' : '只能'}选择一条数据`);
            }else{
                let row = rows[0];
                U.qconfim(`确认撤销该操作吗？`).then(r => {
                    if(r){
                        orderSer.cancelPayDebt({cancelPayDebtNum: row.operMark }).then(res => {
                            if(res.code !== 1){
                                U.qerror(res.msg);
                            }else{
                                U.qsuccess(res.msg);

                                // 刷新列表
                                that.getDgList();
                                // 撤销成功
                                that.isCancel = true;
                            }
                        });
                    }
                });
            }
        },

    },
 }
</script>